$(document).ready(function () {
    $('#username').val('');
    $('#password').val('');

    var isLogin = localStorage.getItem('sessionId');
    if (isLogin != null && isLogin != "") {

        window.location.href = "entry.html";
    }
    initSize()
})


function initSize() {
    //适应不同分辨率
    let fontSize;
    resize();
    window.onresize = resize;

    function resize() {

        let winW = window.innerWidth;

        if (winW < 1366) {
            winW = 1366;
        }
        fontSize = winW / 1920 * 100
        document.getElementsByTagName('html')[0].style.cssText = "font-size:" + fontSize + "px";
    }
}


//登录验证
$(".btn-info").click(function () {
    if ($("#username").val() == '') {
        $('#login-msg').find('label').eq(0).html('请输入用户名');
    } else if ($("#password").val() == '') {
        $('#login-msg').find('label').eq(0).html('请输入密码');
    } else {
        login();
    }

});
//回车确认事件
document.onkeydown = function (event) {
    if (event.keyCode == 13) {
        $(".btn-info").trigger("click");
    }
}


function login() {
    var account = $("#username").val();
    var password = $("#password").val();

    var url = '/rest/fstation/login';
    $.ajax({
        url: url,
        type: "POST",
        contentType: "application/json",
        data: JSON.stringify({
            "account": account,
            "password": password
        }),
        success: function (data) {
            var status = data.status;
            var data = data.result;
            if (status == 200) {
                localStorage.setItem('userInfo', JSON.stringify(data));
                localStorage.setItem('sessionId', JSON.stringify(data.sessionId));




                $.ajax({
                    url:  '/rest/fstation/menu/role',
                    type: 'get',
                    data: {
                        roleId: JSON.parse(localStorage.getItem("userInfo")).roleId,
                    },
                    dataType: 'json',
                    contentType: "application/json",
                    headers: {
                        SessionId: JSON.parse(localStorage.getItem('sessionId'))
                    },
                    success: function (res) {
                        const menuData = buildTree(res.result);

                        localStorage.setItem('menu', JSON.stringify(menuData));
                        window.location.href = "../html/entry.html";

                    },
                });



            } else {
                $('#login-msg').find('label').eq(0).html('用户名或密码错误');
            }
        },
        error: function (result) {

        }
    })

}



// 将扁平数据转换为树形结构
function buildTree(data) {

    const idMapping = data.reduce((acc, el, i) => {
        acc[el.id] = i;
        return acc;
    }, {});

    let root = [];
    data.forEach(el => {
        // 判断是否为顶层节点
        if (el.pId === null || el.pId == "") {
            root.push(el);
            return;
        }
        // 找到父节点并添加到它的 children 数组中
        const parentEl = data[idMapping[el.pId]];
        parentEl.children = [...(parentEl.children || []), el];
    });
    return root;
}
